import cv2 as cv
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image

root = tk.Tk()
root.geometry("600x700")
root.title("切割照片")

src_FILE = None


def choose_image():
    global src_FILE
    src_FILE = filedialog.askopenfilename(initialdir="images/", title="选择图片",
                                          filetypes=(("JPEG files", "*.jpg"), ("PNG files", "*.png")))
    img = Image.open(src_FILE)
    #Image size (352394224 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.
    img = img.resize((400, 400), Image.LANCZOS)
    img = ImageTk.PhotoImage(img)
    panel.configure(image=img)
    panel.image = img


def do_image():
    # 导入资源
    global image
    image = cv.imread(src_FILE)  # r"D:\data\IMG_20230719_105059.jpg"
    src_left = image[:, 0:image.shape[1] // 2]
    cv.imshow("left", src_left)
    #cv.imwrite(r"D:\data\pano3\IMG_20230412_095643l.jpg", src_left)
    cv.waitKey()
    src_right = image[:,image.shape[1]//2:image.shape[1]]
    cv.imshow("right", src_right)
    #cv.imwrite(r"D:\data\pano3\IMG_20230412_095643r.jpg", src_left)
    cv.waitKey()
    cv.destroyAllWindows()


if __name__ == "__main__":
    button = tk.Button(root, text="选择图片", command=choose_image)
    button.pack(side="top", pady=5)

    panel = tk.Label(root)
    panel.pack(side="top", expand="yes", padx="50", pady="50")

    button_do = tk.Button(root, text="切割为2部分", command=do_image)
    button_do.pack(side="bottom", pady=50)

    root.mainloop()
